package com.zk.controller.BasicController;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zk.entity.ResultDto;
import com.zk.entity.User;
import com.zk.service.IUserService;
import com.zk.utils.VerityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
@Api(tags = "用户注册")
@RestController
public class registerController {
    @Autowired
    @Qualifier("userServiceImpl")
    IUserService iUserService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @ApiOperation(value = "用户注册",notes = "手机号不能重复")
    @PostMapping("/register")
    @ApiImplicitParams({
            @ApiImplicitParam(name="params",dataType = "Map",paramType = "body"),

    })
    public ResultDto<String> register(@RequestBody Map<String,String> params){
        System.out.println(params.values());
        User user = new User();
        user.setUserName(params.get("user_phone"));
        user.setRealName(params.get("real_name"));
        user.setUserAge(Integer.parseInt(params.get("user_age")));
        user.setUserEmail(params.get("user_email"));
        user.setUserGender(Integer.parseInt(params.get("user_gender")));
        user.setUserPhone(params.get("user_phone"));
        user.setUserRole(1);
        user.setUserPwd(params.get("user_pwd"));
        user.setIsDel(0);
        String code = params.get("code");
        System.out.println(params.values());
//        String redisCode = redisTemplate.opsForValue().get("code");
        String redisCode= redisTemplate.opsForHash().get(user.getUserPhone(),"code").toString();
        if(redisCode==null)
        {
            System.out.println("我为空");
        }
        System.out.println(redisCode+"======>>>");

        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_phone",user.getUserPhone());
        User u1 = iUserService.getOne(queryWrapper);

        if(u1!=null)
        {
            return new ResultDto<>(500,"手机号已经注册");

        }
        if(redisCode==null)
        {
            return new ResultDto<>(500,"验证码以过期请重新申请");

        }
        if(!code.equals(redisCode)){
            return new ResultDto<>(500,"验证码错误");

        }
        System.out.println(user.toString()+"====>>>");
        user.setUserName(user.getUserPhone());
        String password = user.getUserPwd();
        Map<String,String> map = VerityUtil.encrypted(password);
        user.setSalt(map.get("salt"));
        user.setUserPwd(map.get("MD5password"));
        System.out.println("=====================>>>");
        boolean bool = false;
        try{
             bool = iUserService.save(user);

        }catch (Exception e){
            e.printStackTrace();

        }

      if(bool)
      {
          return new ResultDto<>(200,"恭喜注册成功请登录");
      }
      else{
          return new ResultDto<>(500,"系统繁忙请稍后重试");
      }

    }


}
